//这里要获取X轴滚动条位置
function getPageScroll() {
    var x ;
    if (window.pageYOffset) {    // all except IE
        x = window.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {    // IE 6 Strict
        x = document.documentElement.scrollLeft;
    } else if (document.body) {    // all other IE
        x = document.body.scrollLeft;
    }
    return x;
}
//这里要获取Y轴滚动条位置
function getPageScrollY() {
    var  y;
    if (window.pageYOffset) {    // all except IE
        y = window.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {    // IE 6 Strict
        y = document.documentElement.scrollTop;
    } else if (document.body) {    // all other IE
        y = document.body.scrollTop;
    }
    return y;
}


function clearData() {//清除选框，包括选中的数据
    $("#selectDiv").remove();
    chooseArr = [];
}

var chooseArr = [];//这里是最终的选择的数据
$(function () {
    $("[name='choose']").on("click", function () {
        chooseArr = [];
        var selectflag = $("#selectflag").val();
        if(selectflag==1) {
            //说明现在是框选
        }else{//是单选
            var  evt = window.event || arguments[0];
            clearEventBubble(evt);
            chooseArr.push($(this)[0]);//单个选中的数据
            tbgsAuto();//自动填报工时 // 单个选中
        }

    });
});

(function () {


    //指定范围内 onmousedown , (  document 是 对整个页面)
    var optAreaDiv = document.getElementById("optAreaDiv");
    optAreaDiv.onmousedown = function () {
        var evt = window.event || arguments[0];
        if(evt.button != 0){//只让 数遍左键 起作用 0 左 1 中 2 右
            return;
        }
        var selectDivExists = $("#selectDiv");
        if(selectDivExists && selectDivExists.html()!=null ) {
            clearData();
        }
        var selList = $("[name='choose']");
        var isSelect = true;
        var pageScroll =  parseInt(getPageScroll());
        var pageScrollY  =  parseInt(getPageScrollY());
        var startX = (evt.x || evt.clientX) + parseInt(pageScroll);
        var startY = (evt.y || evt.clientY)+ parseInt(pageScrollY);
        var selDiv = document.createElement("div");
        selDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";
        selDiv.id = "selectDiv";
        document.body.appendChild(selDiv);
        selDiv.style.left = startX + "px";
        selDiv.style.top = startY + "px";
        var _x = null;
        var _y = null;
        clearEventBubble(evt);

        optAreaDiv.onmousemove = function() {
            evt = window.event || arguments[0];
            if (isSelect) {
                if (selDiv.style.display == "none") {
                    selDiv.style.display = "";
                }
                _x = (evt.x || evt.clientX)+pageScroll;
                _y = (evt.y || evt.clientY)+pageScrollY;
                selDiv.style.left = Math.min(_x, startX) + "px";
                selDiv.style.top = Math.min(_y, startY) + "px";
                selDiv.style.width = Math.abs(_x - startX) + "px";
                selDiv.style.height = Math.abs(_y - startY) + "px";
                chooseArr = [];//清除以前的
                var _l = selDiv.offsetLeft, _t = selDiv.offsetTop;
                var _w = selDiv.offsetWidth, _h = selDiv.offsetHeight;
                for (var i = 0; i < selList.length; i++) {
                    var sl = selList[i].offsetWidth + selList[i].offsetLeft;
                    var st = selList[i].offsetHeight + selList[i].offsetTop;
                    if (   sl > _l  && st > _t  &&  selList[i].offsetLeft < _l + _w  && selList[i].offsetTop < _t + _h ) {
                        chooseArr.push(selList[i]);
                    }
                }
            }

            clearEventBubble(evt);

        };

        document.onmouseup = function () {
            isSelect = false;
            if (selDiv) {
                document.body.removeChild(selDiv);
            }
            selList = null, _x = null, _y = null, selDiv = null, startX = null, startY = null, evt = null;
            if(chooseArr.length>0){ //判断 chooseArr 的长度,先点击 在阔选 会导致 两次提交
                tbgsAuto();//自动填报工时
                $("#selectflag").val(1);
            }
        }
    }
})();

function clearEventBubble(evt) {
    if (evt.stopPropagation)
        evt.stopPropagation();
    else
        evt.cancelBubble = true;
    if (evt.preventDefault)
        evt.preventDefault();
    else
        evt.returnValue = false;
}

